//
//  LXCustomNavView.m
//  LXLessDemo
//
//  Created by LX on 16/2/12.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXCustomNavView.h"
#import "LXPullDownView.h"
#define navHeight  40
@interface LXCustomNavView(){
}

@end
@implementation LXCustomNavView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.title = title;
    }
    return self;
}

-(void)addMainView{
    self.backgroundColor = [UIColor clearColor];
    
    
}
-(void)settingBackBarButtonWithImage:(UIImage *)image text:(NSString *)test sel:(SEL)sel target:(UIViewController *)target{
    [self.lefftButtonView removeFromSuperview];
    self.lefftButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds)/3, CGRectGetHeight(self.bounds))];
    

    [self addSubview:self.lefftButtonView];
    
    [self settingBarButtonWithAddView:self.lefftButtonView image:image text:test sel:sel target:target isRight:NO];
}
-(void)settingBarButtonWithAddView:(UIView *)addView image:(UIImage *)image text:(NSString *)test sel:(SEL)sel target:(UIViewController *)target isRight:(BOOL)isRight{

    CGFloat xPOINT = 15;
    CGFloat width = 0;
    UILabel *lab;
    test = test ? test:@"     ";
    UIButton *imageView;
    if (image) {
        imageView = [UIButton  buttonWithType:UIButtonTypeCustom];
        [imageView setImage:image forState:0];
        [imageView sizeToFit];
        width = CGRectGetWidth(imageView.bounds);
    }
    if (test) {
        lab = [LXCommonUtils settingLabWithFrame:CGRectMake(0, 0, CGRectGetWidth(addView.bounds) - xPOINT - width - 3, 40) text:test textColor:[UIColor whiteColor] fount:firstTextFont];
        width += CGRectGetWidth(lab.bounds) + 3;
        [lab setUserInteractionEnabled:YES];
    }
    
    CGRect frame = addView.frame;
    if (!isRight) {
        frame.size.width = width;
        addView.frame = frame;
        CGFloat x = 0;
        if (imageView) {
            imageView.center = CGPointMake(CGRectGetWidth(imageView.bounds)/2.0, CGRectGetHeight(addView.bounds)/2.0);
            [addView addSubview:imageView];
            x = CGRectGetMaxX(imageView.frame) + 3;
        }
        if (lab) {
            lab.frame = CGRectMake(0, 0, width , 40);
            [lab setTextAlignment:NSTextAlignmentRight];
            lab.center = CGPointMake(CGRectGetWidth(lab.bounds)/2.0, CGRectGetHeight(addView.bounds)/2.0);
            [addView addSubview:lab];
        }
        addView.center = CGPointMake(CGRectGetWidth(addView.bounds)/2.0 + xPOINT, CGRectGetHeight(self.bounds)/2.0);
    }else{
        frame.size.width = width;
        addView.frame = frame;
        CGFloat x = 0;
        if (lab) {
            lab.frame = CGRectMake(0, 0, width, 40);
            [lab setTextAlignment:NSTextAlignmentRight];

            lab.center = CGPointMake(CGRectGetWidth(lab.bounds)/2.0, CGRectGetHeight(addView.bounds)/2.0);
            [addView addSubview:lab];
            x = CGRectGetMaxX(lab.frame) + 3;
        }
        if (imageView) {
            imageView.center = CGPointMake(CGRectGetWidth(imageView.bounds)/2.0 + x, CGRectGetHeight(addView.bounds)/2.0);
            [addView addSubview:imageView];
        }
        
        addView.center = CGPointMake(CGRectGetWidth(self.bounds) - CGRectGetWidth(addView.bounds)/2 - xPOINT, CGRectGetHeight(self.bounds)/2.0);

    }
    addView.backgroundColor = [UIColor clearColor];
    [addView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target action:sel]];
}
-(void)settingRightBarButtonWithImage:(UIImage *)image text:(NSString *)test sel:(SEL)sel  target:(UIViewController *)target{
    [self.rightButtonView removeFromSuperview];
    self.rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds)-CGRectGetWidth([UIScreen mainScreen].bounds)/3, 0, CGRectGetWidth([UIScreen mainScreen].bounds)/3, CGRectGetHeight(self.bounds))];
    [self addSubview:self.rightButtonView];
    [self settingBarButtonWithAddView:self.rightButtonView image:image text:test sel:sel target:target isRight:YES];

    
}
-(void)btnClick{
    NSLog(@"click");
}
-(void)settingTitleView:(UIView *)view{
    [self.titleButtonView removeFromSuperview];
    self.titleButtonView = view;
    self.titleButtonView.center = CGPointMake(CGRectGetWidth(self.bounds)/2.0, CGRectGetHeight(self.bounds)/2.0);
    [self addSubview:self.titleButtonView];
}
-(void)setTitle:(NSString *)title{
    [self.titleButtonView removeFromSuperview];
    self.titleButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds)/3, navHeight)];
    self.titleButtonView.backgroundColor = [UIColor clearColor];
    self.titleButtonView.center = CGPointMake(CGRectGetWidth(self.bounds)/2.0, CGRectGetHeight(self.bounds)/2.0);
    [self addSubview:self.titleButtonView];
    UILabel *lab = [LXCommonUtils settingLabWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.titleButtonView.bounds), CGRectGetHeight(self.titleButtonView.bounds)) text:title textColor:[UIColor whiteColor] fount:firstTextFont];
    lab.center = CGPointMake(CGRectGetWidth(self.titleButtonView.bounds)/2, CGRectGetHeight(self.titleButtonView.bounds)/2.0);
    [self.titleButtonView addSubview:lab];
}

//-(void)settingTitleBarButtonWithImage:(UIImage *)image text:(NSString *)test sel:(SEL)sel{
//    [self.titleButtonView removeFromSuperview];
//    self.titleButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds)/3, navHeight)];
//    self.titleButtonView.backgroundColor = [UIColor redColor];
//    [self.titleButtonView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:sel]];
//
//    //    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"close"]];
//    [self addSubview:self.titleButtonView];
//
//}

@end
